// pages/goods_list/goods_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [
      {
        id: "0",
        name: "综合",
        isActive: true
      },
      {
        id: "1",
        name: "销量",
        isActive: false
      },
      {
        id: "2",
        name: "价格",
        isActive: false
      }
    ],
    goodsList: [],
    data_src: "http://image4.suning.cn/uimg/b2c/newcatentries/0070179273-000000000613028312_1_400x400.jpg"
  },
  queryInfo: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  getGoodsList: async function () {
    let { data } = await wx.goodsApi.getGoodsList(this.queryInfo)
    console.log(data)
    this.totalpage = Math.ceil(data.message.total / this.queryInfo.pagesize)
    this.setData({
      goodsList: this.data.goodsList.concat(data.message.goods)
    })
  },
  onItemClick: function (e) {
    let { activeId } = e.detail
    let { tabList } = this.data
    tabList.map(item => item.isActive = item.id === activeId)
    this.setData({
      tabList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!options.cid){
      this.queryInfo.cid = ""
    }else{
      this.queryInfo.cid = options.cid
    }
    this.getGoodsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.queryInfo.pagenum = 1
    this.data.goodsList = []
    this.getGoodsList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.queryInfo.pagenum >= this.totalpage) {
      wx.showToast({
        title: '已经是最后一条',
      })
    } else {
      this.queryInfo.pagenum++
      this.getGoodsList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})